
let axios = require("axios")
let crypto = require("crypto")
let fs = require("fs");

let mhyVersion = "2.15.1"
let salt = "xV8v4Qu54lUKrEYFZkJhB8cuOh9Asafs" // Github-@lulu666lulu
let client_type = "5"

let uid = 112491090;  // 105068948_jj  112491090_sx
let serveid1 = "cn_gf01";  // cn_qd01
let Cookie =  "_ga=GA1.2.223977216.1602565067; _MHYUUID=27889a6a-39cf-4fb3-854d-b32dd62a66dc; UM_distinctid=17c15b34e661bc-05ef7d49720a6c-87f133f-1fa400-17c15b34e6859; _gid=GA1.2.1035542548.1636101963; CNZZDATA1275023096=2068864476-1636096446-https%253A%252F%252Fgithub.com%252F%7C1636096446; ltoken=jZsChv8hThdaDXlCiL0gboJmy9jpOUFIgCvMbfeT; ltuid=187094410; cookie_token=XSuWY2woDlab3ywXnIPfcal07uy4i8snRD9O7coJ; account_id=187094410; _gat=1";
// index?server=" + serveid1 + "&role_id=" + uid
// spiralAbyss?server=" + serveid1 + "&role_id=" + uid + "&schedule_type=1"
// (async () => {  // 主页
//     let res = await axios.get("https://api-takumi.mihoyo.com/game_record/app/genshin/api/index?server=" + serveid1 + "&role_id=" + uid, {
//         headers: {
//             'Accept': 'application/json, text/plain, */*',
//             'DS': DSGet("role_id=" + uid + "&server=" + serveid1),
//             'Origin': 'https://webstatic.mihoyo.com',
//             'x-rpc-app_version': mhyVersion,
//             'User-Agent': 'Mozilla/5.0 (Linux; Android 9; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 miHoYoBBS/'+mhyVersion,  // 2.2.0
//             'x-rpc-client_type': client_type,
//             'Referer': 'https://webstatic.mihoyo.com/app/community-game-records/index.html?v=6',
//             'Accept-Encoding': 'gzip, deflate',
//             'Accept-Language': 'zh-CN,en-US;q=0.8',
//             'X-Requested-With': 'com.mihoyo.hyperion',
//             "Cookie": Cookie
//         }
//     })
//     console.log(res);
//     if(res.data && res.data.data) {
//         fs.writeFileSync("./"+uid+"主页.json", JSON.stringify(res.data.data))        
//     }
// })()

// (async () => {  // 深渊
//     let res = await axios.get("https://api-takumi.mihoyo.com/game_record/app/genshin/api/spiralAbyss?server=" + serveid1 + "&role_id=" + uid + "&schedule_type=1", {
//         headers: {
//             'DS': DSGet("role_id=" + uid + "&schedule_type=" + 1 + "&server="+ serveid1),
//             'Origin': 'https://webstatic.mihoyo.com',
//             'Cookie': Cookie,                
//             'x-rpc-app_version': mhyVersion,
//             'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/' + mhyVersion,  // 2.11.1
//             'x-rpc-client_type': client_type,
//             'Referer': 'https://webstatic.mihoyo.com/'
//         }
//     })
//     console.log(res);
//     if(res.data) {
//         fs.writeFileSync("./"+uid+"深渊.json", JSON.stringify(res.data))        
//     }
// })()

(async () => {  // 角色
    let res = await axios({
        url:"https://api-takumi.mihoyo.com/game_record/app/genshin/api/character",
        method:"post",
        headers:{
            'DS': DSGet('',{"character_ids": [10000046,10000048] ,"role_id": uid ,"server": serveid1}),
            'Origin': 'https://webstatic.mihoyo.com',
            'Cookie': Cookie,
            'x-rpc-app_version': mhyVersion,
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/' + mhyVersion,
            'x-rpc-client_type': '5',
            'Referer': 'https://webstatic.mihoyo.com/'
        },
        data: {
            "character_ids": [10000046,10000048] ,"role_id": uid ,"server": serveid1
        }
    })
    console.log(res);
    if(res.data) {
        fs.writeFileSync("./"+uid+"角色.json", JSON.stringify(res.data))        
    }
})()

// (async () => {  // 每日
//     let res = await axios.get("https://api-takumi.mihoyo.com/game_record/app/genshin/api/dailyNote?server=" + serveid1 + "&role_id=" + uid, {
//         headers: {
//             'DS': DSGet("role_id=" + uid + "&server=" + serveid1),
//             'Origin': 'https://webstatic.mihoyo.com',
//             'Cookie': Cookie,                
//             'x-rpc-app_version': mhyVersion,
//             'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) miHoYoBBS/' + mhyVersion,  // 2.11.1
//             'x-rpc-client_type': client_type,
//             'Referer': 'https://webstatic.mihoyo.com/'
//         }
//     })
//     console.log(res);
//     if(res.data) {
//         fs.writeFileSync("./"+uid+"每日.json", JSON.stringify(res.data))        
//     }
// })()


function DSGet(query,br) {
    let n = salt
    let i = ((new Date().getTime()) / 1000).toFixed(0)
    let r = (Math.random()*100000 + 100001).toFixed(0)
    let b = ""
    if(br) {
        b = JSON.stringify(br)        
    }
    let q = query
    let c = md5("salt=" + n + "&t=" + i + "&r=" + r + "&b=" + b + "&q=" + q)
    let res = i + "," + r + "," + c;
    return res
}

function md5(str) {
    return crypto.createHash('MD5').update(str).digest('hex');
}




// def DSGet(query:str):
//     n = salt
//     i = str(int(time.time()))
//     r = str(random.randint(100001, 200000))
//     b = ""
//     q = query
//     c = md5("salt=" + n + "&t=" + i + "&r=" + r + "&b=" + b + "&q=" + q)
//     return i + "," + r + "," + c


// '''
// {body:"",query:{"action_ticket": undefined, "game_biz": "hk4e_cn”}}
// 对应 https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn //查询米哈游账号下绑定的游戏(game_biz可留空)
// {body:"",query:{"uid": 12345(被查询账号米哈游uid)}}
// 对应 https://api-takumi.mihoyo.com/game_record/app/card/wapi/getGameRecordCard?uid=
// {body:"",query:{'role_id': '查询账号的uid(游戏里的)' ,'server': '游戏服务器'}}
// 对应 https://api-takumi.mihoyo.com/game_record/app/genshin/api/index?server= server信息 &role_id= 游戏uid
// {body:"",query:{'role_id': '查询账号的uid(游戏里的)' , 'schedule_type': 1(我这边只看到出现过1和2), 'server': 'cn_gf01'}}
// 对应 https://api-takumi.mihoyo.com/game_record/app/genshin/api/spiralAbyss?schedule_type=1&server= server信息 &role_id= 游戏uid
// {body:"",query:{game_id: 2(目前我知道有崩坏3是1原神是2)}}
// 对应 https://api-takumi.mihoyo.com/game_record/app/card/wapi/getAnnouncement?game_id=    这个是公告api
// b=body q=query
// 其中b只在post的时候有内容，q只在get的时候有内容
// '''